//
//  UTString_Query.swift
//  iPhoneTests
//
//  Created by Toj on 8/9/22.
//

import XCTest

class UTString_Query: XCTestCase {
    
    override func setUpWithError() throws {
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }
    
    override func tearDownWithError() throws {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    }
    
    func testExample() throws {
        print("-->UTests: \n start \n")
        var rect = CGRect.zero
        
        rect = CGRect(width: 100)
        // -->UTests: rect: (0.0, 0.0, 100.0, 844.0)
        print("-->UTests: rect: \(rect)")
        
        rect = CGRect(height: 100)
        // -->UTests: rect: (0.0, 0.0, 390.0, 100.0)
        print("-->UTests: rect: \(rect)")
        
        rect = CGRect(fullHeight: 200, y: 100, width: 100)
        // -->UTests: rect: (200.0, 100.0, 100.0, 844.0)
        print("-->UTests: rect: \(rect)")
        
        rect = CGRect(fullWidth: 200, y: 100, height: 100)
        // -->UTests: rect: (200.0, 100.0, 390.0, 100.0)
        print("-->UTests: rect: \(rect)")
        
        
        
        
        var targetURL = ""
        
        // testQueryString()
        // testToQueries()
        let param = ["a": "1", "b": "2"]
        // Player?a=1&b=2
        targetURL = "Player?".url_append(queries: param)
        print("-->UTests: queries: \(targetURL)")
        
        // Player?a=1&b=2
        targetURL = "Player?a=1&".url_append(queried: param)
        print("-->UTests: queries: \(targetURL)")
        
        // Player?a=1&b=2
        targetURL = "Player?a=1".url_append(queried: "b=2")
        print("-->UTests: queries: \(targetURL)")
        
        let parameters = "Player?a=1&b=2".url_queryParameters
        let query = parameters?.to_queryString ?? ""
        print("-->UTests: queries: \(query)")
        
        print("-->UTests: \n end \n")
    }
    
    func testPerformanceExample() throws {
        // This is an example of a performance test case.
        measure {
            // Put the code you want to measure the time of here.
        }
    }
}

// UT Case
extension UTString_Query {
    
    /**
     * queries to dictionary
     * e.g.: card=1234&name=tao&age=11
     * retsult: ["card": "1234", "age":"11", "name":"tao"]
     */
    func testToQueries() {
        let queries = "card=1234&name=tao&age=11".to_queries
        print("-->UTests: queries: \(queries)")
    }
    
    private func testQueryString() {
        
        var queryString = "utm_source=h5&utm_medium=1&utm_campaign=1&phone=255696532100&decoder=120489&smartCard=01837741102&linkStatus=pre-link&model=ACTIVITY_LINK"
        queryString = "https://deeplink.startimes.me/?link=https://platformapi/webtoapp?callup_referrer=utm_source%3Dh5%26utm_medium%3D1%26utm_campaign%3D1%26phone%3D255696532100%26decoder%3D120489%26smartCard%3D01837741102%26linkStatus%3Dpre-link%26model%3DACTIVITY_LINK&apn=com.star.mobile.video&amv=60000&isi=1168518958&ibi=com.startimes.onlinetv"
        
        var urlqueries = queryString.url_queries ?? [:]
        print("-->UTests: queries url_queries:\(urlqueries)")
        
        queryString = "startimes://platformapi/webtoapp?target=c3RhcnRpbWVzOi8vcGxheWVyP3Byb2dyYW1EZXRhaWxJZD0xMDEyNyZ2b2RJZD0yMTU5OTQ=&callup_referrer=utm_term%3Dqa.startim.es%252F0hy2%26utm_source%3DOperationPromotion%26utm_medium%3Dtvprogram2%26utm_campaign%3Dtv"
        // queryParameters(query: queryString)
        urlqueries = queryString.url_queries ?? [:]
        print("-->UTests: queries url_queries:\(urlqueries)")
        
//        let queries = queryString.to_queries
//        print("-->UTests: queries to_queries:\(queries)")
    }
    
    func queryParameters(query: String) -> [String: String] {
        var parameters = [String: String]()
        let components = query.components(separatedBy: "&")
        components.forEach {
            let keyAndValue = $0.components(separatedBy: "=")
            if keyAndValue.count == 2 {
                let key = keyAndValue[0]
                let value = keyAndValue[1].replacingOccurrences(of: "+", with: " ").removingPercentEncoding
                ?? keyAndValue[1]
                parameters[key] = value
            }
        }
        return parameters
    }
}
